package com.open.capacity.user.controller;

import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.Notice;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.user.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/notice")
@Api(tags = "通知模块")
public class NoticeController {

    @Autowired
    NoticeService noticeService;

    @GetMapping("/getSelfPublishNotice")
    @ApiOperation(value = "获取自己发布的通知")
    public PageResult getSelfPublishNotice(@RequestParam Map map) {
        try {
            return noticeService.getSelfPublishNotice(map);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }

    }

    @GetMapping("/getNoReadNoticeCount")
    @ApiOperation(value = "获取未读通知数")
    public Result getNoReadNoticeCount(Long userid) {
        try {
            return noticeService.getNoReadNoticeCount(userid);
        }catch (ServiceException e){
            throw new ServiceException(e);
        }
    }

    @GetMapping("/getReceivedAndMarkNoReadNotice")
    @ApiOperation(value = "获取自己所收到的通知并标记未读通知")
    public Result getReceivedAndMarkNoReadNotice(Long userid) {
        try {
            return noticeService.getReceivedAndMarkNoReadNotice(userid);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }

    @PostMapping("/readNotice")
    @ApiOperation(value = "阅读通知")
    public Result readNotice(@RequestParam("nticid") Integer nticid, @RequestParam("userid") Long userid) {
        try {
            return noticeService.readNotice(nticid, userid);
        }catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    @PostMapping("/publishOrUpdateNotice")
    @ApiOperation(value = "发布通知")
    public Result publishOrUpdateNotice(@RequestBody Notice notice) {
        try {
            return noticeService.publishOrUpdateNotice(notice);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }

    }

    @GetMapping("/deleteNotice")
    @ApiOperation(value = "删除通知")
    public Result deleteNotice(Integer nticid) {
        try {
            return noticeService.deleteNotice(nticid);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }

    }
}
